package com.sias.springboot;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.*;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
 * @author 吴文杰
 * @version 1.0
 * @since 2023/6/22 18:08
 */
@SuppressWarnings("all")
public class A39_2 {
  public static void main(String[] args) {
    SpringApplication application = new SpringApplication();

    application.addInitializers(new ApplicationContextInitializer<ConfigurableApplicationContext>() {
      @Override
      public void initialize(ConfigurableApplicationContext applicationContext) {
        System.out.println("执行初始化器增强");
      }
    });

    System.out.println("2.封装启动args");

    System.out.println("8.创建容器");
    GenericApplicationContext context = createApplicationContext(WebApplicationType.SERVLET);
    System.out.println("9.准备容器");
    for (ApplicationContextInitializer initializer : application.getInitializers()) {
      initializer.initialize(context);
    }

    System.out.println("10.加载beandefinition");
    DefaultListableBeanFactory factory = context.getDefaultListableBeanFactory();
    AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(factory);
    reader.register(Config.class);

    XmlBeanDefinitionReader reader1 = new XmlBeanDefinitionReader(factory);
    reader1.loadBeanDefinitions(new ClassPathResource("b03.xml"));


    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(factory);
    scanner.scan("com.sias.springboot.config");

    System.out.println("11.refresh 容器");
    context.refresh();

    for (String name : context.getBeanDefinitionNames()) {
      System.out.println("name:" + name + "\t来源:" + context.getBeanFactory().getBeanDefinition(name).getResourceDescription());
    }
    System.out.println("12.执行runner");

  }

  public static GenericApplicationContext createApplicationContext(WebApplicationType type) {
    GenericApplicationContext context = null;
    switch (type) {
      case SERVLET:
        context = new AnnotationConfigServletWebServerApplicationContext();
        break;
      case REACTIVE:
        context = new AnnotationConfigReactiveWebServerApplicationContext();
        break;
      case NONE:
        context = new AnnotationConfigApplicationContext();
        break;
    }
    return context;
  }

  static class Bean4 {

  }

  static class Bean5 {

  }

  @Configuration
  public static class Config {

    @Bean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
      return new TomcatServletWebServerFactory();
    }
  }
}
